package com.iengoo.inductionms.ctrl.rest;

import com.iengoo.inductionms.entity.AdditionalInformation;
import com.iengoo.inductionms.entity.Employee;
import com.iengoo.inductionms.model.Condition;
import com.iengoo.inductionms.service.AdditionalInformationService;
import com.iengoo.inductionms.service.DepartmentService;
import com.iengoo.inductionms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-03-08 21:18
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    private Map<Object, Object> result = new HashMap<>();
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private AdditionalInformationService additionalInformationService;

    //查询资料报道完成而没有进行信息录入的员工("员工状态为"1")
    @GetMapping
    public List<Employee> query(Condition condition) {
        //condition.setType("1");// TODO
        return employeeService.query(condition);
    }

    @PostMapping
    public void create(@RequestBody Employee employee) {
        employeeService.create(employee);
    }

    @PutMapping("/{id:\\d+}")
    public void update(@PathVariable Integer id, @RequestBody Employee employee) {
        employeeService.update(employee);
    }

    //获取员工工号
    @GetMapping("/{documentSimple:.*}")
    public Map<Object, Object> getEmployeeId(@PathVariable String documentSimple) {
        String employeeId = departmentService.queryDepartmentNum(documentSimple);
        result.clear();
        result.put("employeeId", employeeId);
        return result;
    }

    //删除员工(冻结,将员工状态修改为"2")
    @DeleteMapping("/{id:\\d+}")
    public void delete(@PathVariable int id) {
        Employee employee = new Employee();
        employee.setId(id);
        employeeService.frozenAccounts(employee);
    }

    //获取员工录入信息进行修改
    public void updateInformation(Employee employee, AdditionalInformation additionalInformation) {
        //获取员工id;
        Integer id = employee.getId();
        //员工填写数据传入后台,修改员工状态为"3",表示员工信息填写完成
        employee.setType("3");
        //修改员工基本信息
        employeeService.updateInformation(employee);
        //增加员工附加信息
        //员工id与附加信息表的userID对应,保证一个员工id对应一条附加信息记录
        additionalInformation.setUserId(id);
        additionalInformationService.insert(additionalInformation);
    }

    //获取员工工号判断是否重复
    @GetMapping("/id/{employeeId}")
    public Map<Object, Object> repeat(@PathVariable String employeeId) {
        List<Employee> employeeList = employeeService.queryByEmployeeId(employeeId);
        result.clear();
        //如果employeeList为空则不重复,否则重复
        if (employeeList.size() == 0) {//不重复
            result.put("employeeId", employeeId);
            result.put("result", "success");
            return result;
        } else {//重复
            result.put("employeeId", employeeId);
            result.put("result", "fail");
            return result;
        }
    }

    /**
     * 移动端根据员工id 查询员工所有的信息
     * @param id
     * @return
     */
    @GetMapping("/status/{id}")
    public Map<Object, Object> findById(@PathVariable int id) {
        result.clear();
        result.put("emp", employeeService.findById(id));
        result.put("result", "success");
        return result;
    }
}
